﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using Worktool.CODE_Chris.DataSourceHelpers;
using Worktool.CODE_Chris.SQL_DATA;

namespace Worktool
{
    public partial class Stammdaten : Form
    {
        private SQL sql;
        private Boolean isInit = true;
        private StammdatenHelper helper;

        public Stammdaten()
        {
            InitializeComponent();
            this.Text = "Stammdaten: " + Login.currentDBName; 
            init();
        }

        private void init()
        {
            sql = new SQL();
            helper = new StammdatenHelper(dataGridView1, this);

            sql.sendSQL("dbo.updateStammdatenUsed");
            sql.sendSQL("dbo.updateStammdatenExtended");

            fillElementsInit();

            fillTablesInit();

            initFilterSettings();

            isInit = false;
        }

        #region Init
        private void fillTablesInit()
        {
            try
            {
                helper.getData1();
                label2.Text = dataGridView1.RowCount.ToString();
            }
            catch (Exception e)
            {

            }
        }

        private void fillElementsInit()
        {
            String[,] manufacturer = sql.receiveSQL("SELECT Manufacturer FROM vw_std_manufacturer");
            comboBox1.Items.Clear();
            comboBox1.DropDownHeight = 500;

            for (int a = 0; a < manufacturer.GetLength(1); a++)
            {
                comboBox1.Items.Add(manufacturer[0, a]);
            }
            comboBox1.Refresh();
            comboBox1.Text = "";
        }

        private void initFilterSettings()
        {
            if (helper.getTable1() != null) helper.getTable1().DefaultView.RowFilter = string.Format("Manufacturer LIKE '{0}'", (comboBox1.Text + "*"));
        }

        private void Stammdaten_Load(object sender, EventArgs e)
        {
            typeof(DataGridView).InvokeMember("DoubleBuffered", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetProperty, null, dataGridView1, new object[] { true });
        }
        #endregion

        #region Daten neu laden
        public void reloadAllData()
        {
            Cursor.Current = Cursors.WaitCursor;
            this.Enabled = false;
            fillElementsInit();
            fillTablesInit();
            initFilterSettings();
            this.Enabled = true;
            Cursor.Current = Cursors.Default;
        }
        #endregion

        private void comboBox1_TextChanged(object sender, EventArgs e)
        {
            initFilterSettings();
            label2.Text = dataGridView1.RowCount.ToString();
        }
    }
}
